package com.openxt.nbzj.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.widget.Toast;

/**
 * Created by 颖杰_2 on 2016/7/7.
 */
public abstract class OpenxtFragment extends Fragment {
    private String name;

    public String getName() {
        return name;
    }

    public String getActionUrl(){
        return ((OpenxtBaseActivity)getActivity()).getActionUrl();
    }

    public void setName(String name) {
        this.name = name;
    }

    /**
     * Refresh
     */
    public abstract void refresh();

    /**
     * Called when a fragment will be displayed
     */
    public abstract  void willBeDisplayed();

    public void showMsg(String msg) {
        Context mContext = getActivity();
        if(mContext == null)
            return;
        Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
    }

    public void toActivity(final Class<?> cls) {
        toActivity(cls, false);
    }

    public void toActivity(final Class<?> cls, final boolean closeFlag) {
        try {
            final Context mContext = getActivity();
            Intent intent = new Intent();
            intent.setClass(mContext, cls);
            getActivity().startActivity(intent);
            if (closeFlag)
                getActivity().finish();
        } catch (Exception e) {

        }
    }

    public void toActivityForResult(Class<?> cls, Bundle bundle,
                                    int requestCode) {
        try {
            Intent intent = new Intent();
            intent.setClass(getActivity(), cls);
            if (bundle != null)
                intent.putExtras(bundle);
            getActivity().startActivityForResult(intent, requestCode);
        } catch (Exception e) {

        }
    }

    public void toActivity(final Class<?> cls, final Bundle bundle,
                           final boolean closeFlag) {
        try {
            final Context mContext = getActivity();
            Intent intent = new Intent();
            intent.setClass(mContext, cls);
            if (bundle != null)
                intent.putExtras(bundle);
            getActivity().startActivity(intent);
            if (closeFlag)
                getActivity().finish();
        } catch (Exception e) {

        }
    }

    public void toActivity(final Class<?> cls, final Bundle bundle) {
        toActivity(cls, bundle, false);
    }
}
